package gsw.tool.datastructure.table;

import gsw.tool.common.ToolShow;

/**
 * 双链表
 *
 * @param <E>
 */
public class LinkedList<E> extends LinkedTable<E> {
    //尾节点
    Node<E> last;

    public static void test() {
        LinkedList<String> linkedList = new LinkedList<>();
        String s1 = "A";
        String s2 = "B";
        String s3 = "C";
        String s4 = "D";
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        linkedList.add(s4);
        ToolShow.log(linkedList.toString());
        ToolShow.log("--------------------------");

        linkedList.remove(s2);
        linkedList.remove(1);
        ToolShow.log(linkedList.toString());
        ToolShow.log("--------------------------");
    }

    /**
     * 获取节点
     *
     * @param index 下标
     * @return 获取的节点
     */
    @Override
    protected Node<E> node(int index) {
        if (index < 0 || index >= size) {
            return null;
        }
        Node<E> resuleNode = null;
        if (index <= size / 2) {
            for (int i = 0; i <= index; i++) {
                if (i == 0) {
                    resuleNode = first;
                } else {
                    resuleNode = resuleNode.next;
                }
            }
        } else {
            for (int i = size - 1; i >= index; i--) {
                if (i == size - 1) {
                    resuleNode = last;
                } else {
                    resuleNode = resuleNode.pre;
                }
            }
        }
        return resuleNode;
    }

    /**
     * 添加元素：指定位置
     *
     * @param index 添加的位置
     * @param e     添加的元素
     * @return
     */
    @Override
    public boolean add(int index, E e) {
        if (index < 0 || index > size) {
            return false;
        }
        Node<E> newNode = new Node<E>(e, null, null);
        //第一次添加的时候
        if (first == null && last == null) {
            first = newNode;
            last = newNode;
            size++;
            return true;
        }
        Node<E> nowNode = node(index);
        if (index <= 0) {
            newNode.next = first;
            first.pre = newNode;
            first = newNode;
        } else if (index < size) {
            newNode.next = nowNode;
            newNode.pre = nowNode.pre;
            nowNode.pre.next = newNode;
            nowNode.pre = newNode;
        } else {
            newNode.pre = last;
            last.next = newNode;
            last = newNode;
        }

        size++;
        return true;
    }

    /**
     * 删除元素：根据对象
     *
     * @param e
     * @return
     */
    @Override
    public boolean remove(E e) {
        Node<E> nowNode = first;
        while (nowNode != null) {
            if (nowNode.item == e) {
                return remove(nowNode);
            } else {
                nowNode = nowNode.next;
            }
        }
        return false;
    }

    /**
     * 删除元素：根据位置
     *
     * @param index
     * @return
     */
    @Override
    public boolean remove(int index) {
        if (index < 0 || index >= size) {
            return false;
        }

        Node<E> node = node(index);
        return remove(node);
    }

    /**
     * 删除元素：根据节点
     *
     * @param nowNode
     * @return
     */
    public boolean remove(Node<E> nowNode) {
        boolean result = false;
        Node<E> prevNode = nowNode.pre;
        Node<E> nextNode = nowNode.next;
        if (prevNode == null) {
            first = nextNode;
        } else {
            prevNode.next = nextNode;
        }
        if (nextNode == null) {
            last = prevNode;
        } else {
            nextNode.pre = prevNode;
        }
        size--;
        return result;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        Node<E> nowNode = first;
        while (nowNode != null) {
            stringBuffer.append(nowNode.toString());
            nowNode = nowNode.next;
        }
        return stringBuffer.toString();
    }
}
